<?php

namespace common\widgets\grid;

use Yii;
use Closure;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;

/**
 * PortletColumn displays a column of mark text content in a grid view.
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class CellColumn extends DataColumn {

    public $format = 'raw';

    /**
     * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
     * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
     *
     * Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
     */
    public $extendAttribute;

    /**
     * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
     * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
     *
     * Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
     */
    public $imageAttribute;

    /**
     * @var array 组合单元格配置项
     */
    public $extendAttributes = [];

    /**
     * @var array 组合单元格配置项
     */
    public $cellOptions = [];

    /**
     * {@inheritdoc}
     */
    protected function renderDataCellContent($model, $key, $index) {
        $meida = $this->renderCellImageContent($model, $key, $index);
        $body = $this->renderCellBodyContent($model, $key, $index);
        $options = ArrayHelper::getValue($this->cellOptions, 'options', []);
        $tag = ArrayHelper::remove($options, 'tag', 'div');
        Html::addCssClass($options, 'media');
        return Html::tag($tag, $meida . $body, $options);
    }

    /**
     * {@inheritdoc}
     */
    protected function renderCellBodyContent($model, $key, $index) {
        $options = $this->cellOptions;
        //
        $titleOptions = ArrayHelper::remove($options, 'titleOptions', [
            'class' => 'combine-first'
        ]);
        $titleTag = ArrayHelper::remove($titleOptions, 'tag', 'h5');
        $title = Html::tag($titleTag, parent::renderDataCellContent($model, $key, $index), $titleOptions);
        //
        $extendOptions = ArrayHelper::remove($options, 'extendOptions', [
            'class' => 'combine-seconed'
        ]);
        $extendTag = ArrayHelper::remove($extendOptions, 'tag', 'p');
        $extend = Html::tag($extendTag, $this->renderCellExtendContent($model, $key, $index), $extendOptions);
        //
        $tag = ArrayHelper::remove($options, 'tag', 'div');
        Html::addCssClass($options, 'media-body');
        return Html::tag($tag, $title . $extend, $options);
    }

    /**
     * 输出组合内容
     * @param mixed $model
     * @param mixed $key
     * @param array $index
     * @return type
     */
    protected function renderCellExtendContent($model, $key, $index) {
        $extend = $this->extendAttribute;
        if ($extend instanceof Closure) {
            return call_user_func($extend, $model, $key, $index, $this);
        }
        return $this->grid->formatter->format(ArrayHelper::getValue($model, $extend), 'raw');
    }

    /**
     * 输出组合内容图片
     * @param mixed $model
     * @param mixed $key
     * @param array $index
     * @return type
     */
    protected function renderCellImageContent($model, $key, $index) {
        if (!$this->imageAttribute) {
            return null;
        }
        $media = $this->imageAttribute;
        if ($media instanceof Closure) {
            return call_user_func($media, $model, $key, $index, $this);
        }
        $image = ArrayHelper::getValue($model, $media);
        if (empty($image)) {
            return null;
        }
        return $this->grid->formatter->format($image, [
            'image',
            'options' => [
                'frame' => false,
                'default' => false,
            ]
        ]);
    }

}
